package com.leo.m1803.d06;

/**
 * Created by LEO on 2018/3/6.
 */
public class Offer08 {
    public static int findMin(int[] s){
        int i = 0, j = s.length-1;
        while (i<j){
            if (i == j-1){
                return s[j];
            }
            int mid = (i+j)/2;
            if (s[i] == s[mid] && s[j] == s[mid]){
                int temp = s[j];
                while (i <= j){
                    if (s[j] < temp){ temp = s[j]; }
                    else if (s[j] > temp){ return temp; }
                    j--;
                }
            }else if (s[mid] > s[i]){
                i = mid;
            }else if (s[mid] < s[i]){
                j = mid;
            }
        }
        return s[j];
    }

    public static void main(String[] args) {
        System.out.println(findMin(new int[]{4,5,6,7,1,2}));
        System.out.println(findMin(new int[]{4,5,6,7,1,2,3}));
        System.out.println(findMin(new int[]{2,2,2,2,7,1,2,2}));
    }
}
